package com.ls.herostory.util;

import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.Type;

/**
 * @author liangsu
 * @version v1.0
 * @Description
 * @Date 2020/10/12 14:56
 * @since
 */
public class BeanUtils {

    public static  <T>T instance(Class<T> clazz) {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     *  获取实现泛型接口类的真实泛型参数
     * @param clazz
     * @param interfaceClass
     * @return
     */
    public static Type[] getInterfaceArgs(Class clazz, Class interfaceClass){
        Type[] interfacesTypes = clazz.getGenericInterfaces();
        for(Type type : interfacesTypes){
            if(((ParameterizedTypeImpl) type).getRawType().isAssignableFrom(interfaceClass)){

                return ((ParameterizedTypeImpl) type).getActualTypeArguments();
            }
        }
        return null;
    }

}
